package com.smy.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smy.reggie.common.CustomException;
import com.smy.reggie.dao.CategoryMapper;
import com.smy.reggie.pojo.Category;
import com.smy.reggie.pojo.Dish;
import com.smy.reggie.pojo.Setmeal;
import com.smy.reggie.service.CategoryService;
import com.smy.reggie.service.DishService;
import com.smy.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        //判断是否有关联菜品
        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
        dishWrapper.eq(Dish::getCategoryId,id);
        Long count = dishService.count(dishWrapper);
        if(count>0)throw new CustomException("该分类已有关联菜品！");
        //判断是否有关联套餐
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.eq(Setmeal::getCategoryId,id);
        Long count2 = setmealService.count(setmealWrapper);
        if(count2>0)throw new CustomException("该分类已有关联套餐！");
        //调用父类删除方法
        super.removeById(id);
    }
}
